
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject of VATATA.com.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/


#include "miniutil/srtti.h"

#include "minitest/testcase.h"

#include <iostream>

#include "miniutil/tinyxml.h"
#include "miniutil/module.h"

#include "miniutil/strutil.h"
#include "miniutil/log.h"


class srtti_t1
{
public:
	static int count;
	srtti_t1()
	{
		count++;
	}

	char b[11];

} * p_srtti = new srtti_t1();

int srtti_t1::count = 0;

static  int num = miniutil::srtti::srtti::_declare<srtti_t1>( "rtti_srtti_t1" );

class srtti_testcase_1 : public testcase
{

public: 
	srtti_testcase_1()
	{
		themain.tests.push_back( this );
	}

	virtual void test()
	{
		cout << "srtti_testcase_1 test" << std::endl;

		miniutil::srtti::manager::_set( "srtti_t1_instance", p_srtti );

		srtti_t1 * p_get = (srtti_t1 *)miniutil::srtti::manager::_get( "srtti_t1_instance" );
		cout << "\ttest set/get instance...\t\t";

		if ( p_get == p_srtti )
			cout << "OK" << std::endl;
		else
			cout << "Fail" << std::endl;

		cout << "\ttest set/get instance 2...\t\t";

		p_get = NULL;
		p_get = miniutil::srtti::manager::_get( "srtti_t1_instance", p_get);
		if ( p_get == p_srtti )
			cout << "OK" << std::endl;
		else
			cout << "Fail" << std::endl;


		cout << "\ttest class_worker compile ...\t\t";

		miniutil::srtti::class_worker< srtti_t1 > *p_worker  = new miniutil::srtti::class_worker< srtti_t1 >();

		srtti_t1::count = 987;
		p_get = (srtti_t1 *) p_worker->_new();

		if ( srtti_t1::count == 988 )
			cout << "OK"  << std::endl;
		else
			cout << "Fail" << std::endl;

		cout << "\ttest rtti compile ...\t\t";
		srtti_t1::count = 3452 ;

		p_get = (srtti_t1 *) miniutil::srtti::srtti::_newInstance( "rtti_srtti_t1" );

		if ( srtti_t1::count == 3453 )
			cout << "OK"  << std::endl;
		else
			cout << "Fail" << std::endl;

		cout << "\ttest rtti compile 2...\t\t";
		srtti_t1::count = 3452 ;
		miniutil::srtti::srtti::_newInstance( "rtti_srtti_t1" ,  p_get);

		if ( srtti_t1::count == 3453 )
			cout << "OK"  << std::endl;
		else
			cout << "Fail" << std::endl;

		cout << "\ttest rtti dll...\t\t";
#ifdef WIN32
		int res = miniutil::srtti::srtti::_declare( "srttidll.dll", "test_srtti" );
#else
		//int res = miniutil::srtti::srtti::_declare( "srttidll.so", "test_srtti" );
		int res = miniutil::srtti::srtti::_declare( "/home/wb/work/miniutil/bin/srttidll.so", "test_srtti" );
#endif
		if ( res == -2 )
			cout << "Fail, version error, dll's srtti version is different" << std::endl;
		else if ( res == -1 )
			cout << "Fail, can not load dll" << std::endl;
		else if ( res < 0 )
			cout << "Fail, generic error" << std::endl;
		else
		{
			miniutil::srtti::inf * p_ok = (miniutil::srtti::inf *)miniutil::srtti::srtti::_newInstance( "test_srtti" );

			if ( p_ok && p_ok->classname == "ok" )
				cout << "OK"  << std::endl;
			else
				cout << "Fail" << std::endl;

		}

		cout << "\ttest module load ...\t\t";

		const char * p_xmlstr = "<modules path='' comment='moduless path'>\n<module classname='test1' dllname='' />\n<module classname='test_srtti' dllname='srttidll.so' />\n</modules> " ;

		miniutil::tinyxml::TiXmlDocument doc;
		
		if ( doc.Parse( p_xmlstr ) == NULL )
		{
			cout << "Failed parse xml data "<< std::endl;
			cout << p_xmlstr << endl;
		}
		else
		{
			miniutil::tinyxml::TiXmlElement * root = doc.RootElement();
			if ( root == NULL )
			{
				cout << "Failed parse xml data to get root element "<< std::endl;
			}
			else if ( stricmp( "modules", root->Value() ) != 0 )
			{
				cout << "Failed, root element is not modules "<< std::endl;
			}
			else
			{
				int i = miniutil::srtti::module::load( root );

				if ( i <= 0 )
				{
					cout << "Failed, load " << i << " modules " << std::endl;
				}
				else
				{
					cout << "OK, load " << i << " modules " << std::endl;
				}
			}
		}

	}
};

testcase * t_srtti_1  = new srtti_testcase_1() ;
